package com.llh.controller.http;

import com.llh.bean.OpenDay;
import com.llh.controller.http.common.CommonExcuteTrade;
import com.llh.mapper.OpenDayMapper;
import com.llh.util.SqlSessionUtil;
import lombok.Data;
import org.apache.ibatis.session.SqlSession;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;

import java.lang.reflect.Field;
import java.text.SimpleDateFormat;
import java.util.LinkedList;
import java.util.List;

/**
 * 交易日历接口
 */
public class TradeCal extends CommonExcuteTrade {
    static Logger logger = LogManager.getLogger(TradeCal.class.getName());

    @Override
    public void parseParam(String[][] items, String[] fields) throws Exception {
        SqlSession sqlSesion = null;

        //财务数据接口目前只需要一次返回一条数据
        List<OpenDay> list = (List<OpenDay>) copyProperty(items, fields);
        logger.info("即将向open_day表中插入:" + list.size() + "条数据");
        try {
            sqlSesion = SqlSessionUtil.getSqlSesion();
            OpenDayMapper mapper = sqlSesion.getMapper(OpenDayMapper.class);
            logger.info("插入数据");
            int i = mapper.insertBatch(list);
            logger.info("插入操作成功,插入数据:" + i + "条");
        }catch (Exception e){
            e.printStackTrace();
            throw new Exception("将数据插入open_day表数据库操作出错出错");

        } finally {
            if(sqlSesion!=null){
                sqlSesion.close();
            }
        }


    }

    @Override
    public List copyProperty(String[][] items, String[] fields) throws Exception {
        List<OpenDay> openDayList = new LinkedList<>();
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
        for (int j = 0; j < items.length; j++) {
            OpenDay openDay = new OpenDay();
            Field[] declaredFields = openDay.getClass().getDeclaredFields();
            //参数值数组
            String[] item = items[j];
            //数据值校验
            if (item.length != fields.length) {
                throw new Exception("api响应的数据有误");
            }
            for (int i = 0; i < fields.length; i++) {
                //参数名
                String field = fields[i];
                //参数值
                String value = item[i];
                for (Field declaredField : declaredFields) {
                    declaredField.setAccessible(true);
                    String fieldName = declaredField.getName();
                    if (field.equals(fieldName)) {
                        //需要转换成date类型的数据
                        if (field.equals("cal_date") || field.equals("pretrade_date")) {
                            //将字符串转换成date.并赋值
                            if (!(value == null)) {
                                declaredField.set(openDay, format.parse(value));
                            }
                        } else {
                            //字符串直接赋值
                            declaredField.set(openDay, value);
                        }

                    }
                }
            }
            //添加到list集合
            openDayList.add(openDay);

        }
        return openDayList;
    }

}

